package com.c2c.controller.mall;

import com.c2c.mapper.GoodsMapper;
import com.c2c.po.Goods;
import com.c2c.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/Index")
public class IndexController {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsService  goodsService;


    @RequestMapping("/seachers")
    public  String findGoods(HttpServletRequest request,String string){
        List<Goods> goodsList=goodsService.findBySomething(string,0);
        request.setAttribute("goodsList",goodsList);
        return "products";
    }

    @RequestMapping("/Lead")
    public String leadCareCategory(HttpServletRequest request, Integer goodsCategoryId){
        List<Goods> goodsList=goodsService.findByClassifyId(goodsCategoryId);
        request.setAttribute("goodsList",goodsList);
        return "products";
    }

    @RequestMapping("/push")
    public  String pushGoods(HttpServletRequest request){
        List<Goods> allGoods = goodsService.findAllGoods();
        List<Goods> goods = new ArrayList<>();
        Collections.shuffle(allGoods);
        for (int i = 0; i <4; i++) {
            goods.add(allGoods.get(i));
        }
       request.setAttribute("allGoods",goods);
        return "index";
    }

}
